Welcome back. Sometimes, when creating an inherited class,
you want to create a method that calls the super classes method,
but just does a little bit extra.
Now let me explain an example.
So, let's suppose that I have this pet class and inside of the pet class,
I have a method, feed.
Now suppose that I wanted to create something that inherited from the pet class.
Let's suppose that I wanted to create a class dog.
So I'm just going to draw out the beginnings of that class dog,
and I want it to inherit from pet,
so I'll say pet in parentheses.
Now, let's suppose that whenever I fed my dog,
I wanted it to both called this feed method,
but then let's suppose that every dog instance also has a little extra message.
So, let's suppose that a dog says Arf!
Thanks, whenever pet is called on that dog.
So, if I did feed itself,
if I did this and then let's say that I print out Arf!
Thanks!
So, if I define my dog class this way,
whenever I create a new instance of dog,
and then call feed on that dog,
then all that happens is Arf!
Thanks gets printed out.
Because when I define this feed method,
I am overwriting the feed method in the parent class.
So, this would never get called,
at least not by default,
but there is a way to call it.
So, the way they call the feed method for
the pet class in addition to
printing out this little extra bit of code that I wanted to run.
The way that I would do that is,
I would say pet, capital P, pet.
So, the name of the class,
and then I would say.feed,
and I would call this, on self.
Now, notice that this is calling feed on the pet class.
Normally, when we call feed,
we create a new instance,
so I might say D1 equals new dog,
and normally I would say d1.feed,
then I would pass in no arguments here because D1 is an instance.
Here instead, I'm saying the name of the class directly,
so I'm saying pet.feed,
and since I haven't specified which pet instance I'm calling feed on,
then I need to pass in that particular instance as the first argument to pet dog.
If I tried to say something like self.feed,
here then self.feed is just going refer to the dogs' feed method.
So, again, the way that I'll create a feed method on a subclass,
that has everything that the superclass feed method has,
and more I'll cloud call
the superclass feed method by saying the name of the superclass in this case pet,
and then I'll say whatever method I want to call,
and then I'll pass in that instance directly.
So, in code this is what that might look like.
So, here I have a dog class,
and dog class inherits from pet,
but it has its own feed method.
Inside of that feed method,
I first call pet dog feed,
with whatever instance this feed method is being called on,
and then I print out our thanks after the pet.
dog feed method has been called.
Now this also works for constructors.
So, let's suppose that I have another subclass of pet called, bird.
Let's suppose that the bird class has it's own constructor,
but I want to call the pet classes constructor as well.
Well, the way that I would do that,
is I would say pet._init,
and I would call it with whatever arguments I want to call.
So, in this case, I'll pass in self,
and then I'll pass in the name.
What this is going to do is it's going to call the pet classes constructor,
on this new instance bird.
So, it's going to set name,
hunger, boredom, and sounds.
So, let's do some multiple choice questions.
Okay. So, this question asks,
what would print one print out b1.sounds is run?
So, B1, here is an instance of bird,
and if I'm calling b1.sounds,
then you'll see that's instance variable,
that gets set right here.
So, here we're setting self.sound equals to a copy of self.sounds.
By default every pet has sounds list of [inaudible].
You'll see that the bird class has a class variable sounds set to chirp.
So, when I call pet._init,
when it said self.sounds equal the self.sounds is copy self.sounds,
what that's going to do because B1 is a bird,
is it's going to create a copy of this list of sounds,
and so b1.sounds ends up being chirp.
So again, the reason that it's creating a copy of this list of sounds,
is because we have the superclass pet,
then we have a subclass bird.
So, every bird is a pet,
and then we have an instance of bird,
and that instance is called B1.
Bird has its own value per sounds,
and pet has its own different value for sounds.
Now when we say b1.sounds,
or self.sounds in the constructor equal a copy of self.sounds,
then Python is going to first look for what is the value of self.sounds.
So, let's go back to the constructor.
So when said self.sounds,
here again self is B1.
So, we're setting b1.sounds equal to b1.sounds,
and we're slicing it so that we create a copy of b1.sounds.
When Python is searching for b1.sounds,
here it first searches inside of the instance.
This instance does it have sounds instance variable yet,
and then it searches inside of that instances class,
and so we find chirp,
or a list that only contains chirp as the list of sounds.
So, here we end up creating a copy of that.
So, I mentioned all of that,
just to say that the answer to this,
is C. Let's do another question.
This question asks, for the dog class defined in the earlier window,
what would happen when d1.feed is run,
if the pet.feed self line was deleted?
So, in other words, if we deleted,
or commented out this line,
then what would happen when we called D1,
which is a new instance of dog,
what would happen, if we call d1.feed?
So, if this pet.feet(self) line was deleted,
then the only thing in the dog feed method that actually runs,
is this print statement prints Arf!
Thanks! So, remember that the pet feed method is never called by default,
because this method overrides that method entirely.
So, the only thing that would happen is print Arf!
Thanks! would run.
So, that means that the answer here,
is C. That's all for now, until next time